package com.auth.web.spring;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auth.domain.T_branch;
import com.auth.service.T_branchService;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.common.utils.MyDateUtils;

@Controller
@RequestMapping("/t_branch")

public class T_branchController
{
	private static Logger log = Logger.getLogger(T_branchController.class.getName());

	@Resource
    private T_branchService service;

	private Pg pg;

	/**
	 * 1、获取 T_branch信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_branch")
	@ResponseBody
	public Pg getAllT_branch(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_branch t_branch = new T_branch();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_branch.setProcess_code(request.getParameter("process_code"));
	        }
			if(request.getParameter("companyid")!=null&&!"".equals(request.getParameter("companyid"))){
	        	t_branch.setCompanyid(request.getParameter("companyid"));
	        }
			if(request.getParameter("branchid")!=null&&!"".equals(request.getParameter("branchid"))){
	        	t_branch.setBranchid(request.getParameter("branchid"));
	        }
			if(request.getParameter("deptid")!=null&&!"".equals(request.getParameter("deptid"))){
	        	t_branch.setDeptid(request.getParameter("deptid"));
	        }
			if(request.getParameter("termid")!=null&&!"".equals(request.getParameter("termid"))){
	        	t_branch.setTermid(request.getParameter("termid"));
	        }
			if(request.getParameter("status")!=null&&!"".equals(request.getParameter("status"))){
	        	t_branch.setStatus(request.getParameter("status"));
	        }
			if(request.getParameter("name")!=null&&!"".equals(request.getParameter("name"))){
	        	t_branch.setName(request.getParameter("name"));
	        }
			if(request.getParameter("memo")!=null&&!"".equals(request.getParameter("memo"))){
	        	t_branch.setMemo(request.getParameter("memo"));
	        }
			if(request.getParameter("ext1")!=null&&!"".equals(request.getParameter("ext1"))){
	        	t_branch.setExt1(request.getParameter("ext1"));
	        }
			if(request.getParameter("ext2")!=null&&!"".equals(request.getParameter("ext2"))){
	        	t_branch.setExt2(request.getParameter("ext2"));
	        }
			if(request.getParameter("ext3")!=null&&!"".equals(request.getParameter("ext3"))){
	        	t_branch.setExt3(request.getParameter("ext3"));
	        }
			if(request.getParameter("createtime")!=null&&!"".equals(request.getParameter("createtime"))){
	        	t_branch.setCreatetime(request.getParameter("createtime"));
	        }
			if(request.getParameter("createuserid")!=null&&!"".equals(request.getParameter("createuserid"))){
	        	t_branch.setCreateuserid(request.getParameter("createuserid"));
	        }
			if(request.getParameter("lastupdateuserid")!=null&&!"".equals(request.getParameter("lastupdateuserid"))){
	        	t_branch.setLastupdateuserid(request.getParameter("lastupdateuserid"));
	        }
			if(request.getParameter("parentbranchid")!=null&&!"".equals(request.getParameter("parentbranchid"))){
	        	t_branch.setParentbranchid(request.getParameter("parentbranchid"));
	        }
      	    if(request.getParameter("id")!=null&&!"".equals(request.getParameter("id"))){
	        	t_branch.setId(new Integer(request.getParameter("id")));
	        }else{
	        	t_branch.setId(0);//如果没有值，直接设成从总公司id=0开始取全部数据。
	        }

	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_branch.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_branch.setEnddate(request.getParameter("enddate"));
	        }
	        t_branch.setStartNo( startIndex );
	        t_branch.setEndNo( startIndex + pageSize );
	    	//加工排序
	        List<T_branch> findAll = AuthUtils.sortBranchList( service.findAll(t_branch) );
	        int totalCount = service.findCountAll(t_branch);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_branchList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_branchList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_branch信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_branch")
	@ResponseBody
	public JsonVO saveT_branch(T_branch t_branch, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//设置当前操作用户信息到vo中
		t_branch.setCreateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		t_branch.setCreatetime(MyDateUtils.getDate());
		t_branch.setLastupdatetime(MyDateUtils.getDate());
		t_branch.setLastupdateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		//取得上级机构对应的信息，把机构级别+1，设置到机构级别字段属性中，为新增机构设置默认机构级别值；
		t_branch.setBranchlevel(getBranchLevel(t_branch.getParentbranchid()));

		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(service.save(t_branch)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 根据上级机构level，加工当前机构level；初始化数据中，总公司的机构级别为0；
	 * @param parentBranchid
	 * @return
	 */
	private String getBranchLevel(String parentBranchid){
		//取得上级机构对应的信息，把机构级别+1，设置到机构级别字段属性中，为新增机构设置默认机构级别值；
		T_branch obj = new T_branch();
		obj.setId(new Integer(parentBranchid));
		obj = service.findById(obj);
		int bl = new Integer(obj.getBranchlevel())+1;
		return bl+"";
	}

	/**
	 * 3、修改T_branch信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_branch
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_branch")
	@ResponseBody
	public JsonVO updateT_branch(T_branch t_branch, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//设置当前操作用户信息到vo中
		t_branch.setLastupdateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		t_branch.setLastupdatetime(MyDateUtils.getDate());//此处可以不设该值，因为该值是在sql中使用sysdate;
		t_branch.setBranchlevel(getBranchLevel(t_branch.getParentbranchid()));
		//取得上级机构对应的信息，把机构级别+1，设置到机构级别字段属性中，为新增机构设置默认机构级别值；
		t_branch.setBranchlevel(getBranchLevel(t_branch.getParentbranchid()));

		//如果有特殊值需要处理，在此处手工转换
		//T_branch t_branchTemp = service.findById(t_branch);
		//t_branchTemp.setXXX(t_branch.getXXX());
		//if(service.update(t_branchTemp)){

		if(service.update(t_branch)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 4、根据id查询T_branch单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_branch json格式字符串
	 */
	@RequestMapping("/getT_branch")
	@ResponseBody
	public T_branch getT_branch(Integer id,HttpServletRequest request){
		T_branch t_branch = new T_branch();
		t_branch.setId(id);
		return service.findById(t_branch);
	}

	/**
	 * 5、删除T_branch根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_branch")
	@ResponseBody
	public JsonVO delT_branch(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_branch t_branch = new T_branch();
		t_branch.setId(id);

		if(service.delete(t_branch)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_branch/toT_branchList查询所有数据页面
	 *  return /WEB-INF/views/auth/t_branch/t_branchList.jsp
	 */
    @RequestMapping(value = "/toT_branchList")
    public String toT_branchList() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_branch/t_branchList.jsp------");
        return "auth/t_branch/t_branchList";
    }

	/**
	 * 7、新增、修改、详情页面t_branch/toT_branchCurd跳转
     * auth/t_branch/T_branchCurd.jsp
	 */
    @RequestMapping(value = "/toT_branchCurd")
    public String toT_branchCurd() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_branch/t_branchCurd.jsp------");
        return "auth/t_branch/t_branchCurd";
    }

    /**
	 * 8、批量删除T_branch根据主键id
	 * 是否要实现批量删除，待考虑，基本上用途不大
	 * @param id
	 * @param request
	 * @param response
	@RequestMapping("/delT_branchBatch")
	public String delT_branchBatch(HttpServletRequest request,HttpServletResponse response){
		String[] ids = request.getParameterValues("ids");
		System.out.println("------ids="+ids);
		T_branch t_branch = new T_branch();
		int j=0;
		for(int i =0;i< ids.length;i++){
			j++;
			log.error("------ids[i]="+ids[i]);
			t_branch.setId(new Integer(ids[i]).intValue());
			service.delete(t_branch);
		}
		if(j==ids.length){
			log.debug("------t_branch批量删除成功！------");
		}else{
			log.error("------t_branch批量删除部分成功！部分失败！------");
		}

		return "redirect:/t_branch/getAllT_branch";
	}
*/



}









